<template>
  <div>
    <el-form
      :model="updateForm"
      :rules="rules"
      ref="updateForm"
      label-width="100px"
      class="demo-ruleForm"
      @submit.native.prevent
    >

      <el-row>
        <el-row>
          <el-col>
            <el-form-item label="上级部门" prop="fkParentDeptCode">
              <select-tree :treeList="menuList" placeholder="上级部门"
                           v-model="updateForm.fkParentDeptCode"
                           v-on:select="onSelectDeptCallBack"
                           :props="parentProps"></select-tree>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="部门名称" prop="deptName">
              <el-input v-model="updateForm.deptName" placeholder="请输入部门名称" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="部门编码" prop="deptTempCode">
              <el-input v-model="updateForm.deptTempCode" :readonly="updateForm.id!=undefined" placeholder="请输入部门编码"
                        clearable>
                <template slot="prepend">{{updateForm.fkParentDeptCode}}</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="排序号" prop="orderIndex">
              <el-input-number size="small" v-model="updateForm.orderIndex" :min="0" :max="99" placeholder="请输入排序号"
                               clearable></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-row>
    </el-form>
    <el-dialog :visible.sync="dialogVisible" title="选择图标" append-to-body>
      <icon-select @selected-event="selectIconCallBack"></icon-select>
    </el-dialog>
  </div>
</template>
<script>
  import HttpCode from '@/js/httpCode.js'
  import authTbDeptApi from '@/apis/authApp/authTbDeptApi.js'

  export default {
    components: {
      SelectTree: () => import('@/components/selecttree/SelectTree.vue'),
      IconSelect: () => import('@/components/icon/iconSelect')
    },
    data () {
      return {
        dialogVisible: false,
        updateForm: {
          orderIndex: '0'
        },
        treeList: [],
        parentProps: {
          children: 'children',
          label: 'deptName',
          valueKey: 'deptCode'
        },
        rules: {
          deptName: [
            {required: true, message: '请输入部门名称', trigger: 'blur'},
            {min: 2, max: 20, message: '部门名称2-20', trigger: 'blur'}
          ],
          deptTempCode: [
            {required: true, message: '请输入部门编码字母和数字', trigger: 'blur'},
            {min: 1, max: 1, message: '部门编码只能一位', trigger: 'blur'}
          ]
        }
      }
    },
    created () {
      //可能需要特殊处理，手动赋值
      this.treeList = this.menuList
      let data = JSON.parse(JSON.stringify(this.data))
      if (data.deptCode != undefined && data.deptCode > 0 && data.fkParentDeptCode != undefined && data.fkParentDeptCode.length > 0) {
        data.deptTempCode = data.deptCode.substr(data.fkParentDeptCode.length, data.deptCode.length)
      }
      this.updateForm = data
    },
    methods: {
      selectIcon () {
        this.dialogVisible = true
      },
      selectIconCallBack (code) {
        this.dialogVisible = false
        this.updateForm.iconDefault = code
      },
      submitForm () {
        this.$refs['updateForm'].validate(valid => {
          if (valid) {
            this.submitFormHandle()
          } else {
            this.cancelSubmitForm()
          }
        })
      },
      onSelectDeptCallBack (data) {
        this.updateForm.fkParentDeptName = data.fullDeptName
      },
      submitFormHandle () {
        delete this.updateForm.children
        this.updateForm.deptCode = this.updateForm.fkParentDeptCode + this.updateForm.deptTempCode
        this.updateForm.fullDeptName = this.updateForm.fkParentDeptName + '\\' + this.updateForm.deptName
        if (this.updateForm.id == undefined || this.updateForm.id == '') {
          authTbDeptApi
            .addService(this.updateForm)
            .then(msg => {
              this.submitFormSuccess(msg)
            })
            .catch(msg => {
              if ((msg.cdoe = 400)) {
                this.formError(msg)
              }
              this.cancelSubmitForm()
            })
        } else {
          authTbDeptApi.updateService(this.updateForm)
            .then(msg => {
              this.submitFormSuccess(msg)
            }).catch(msg => {
            if ((msg.cdoe = 400)) {
              this.formError(msg)
            }
            this.cancelSubmitForm()
          })
        }
      },
      submitFormSuccess (msg) {
        this.$store.commit('successSubmitForm', {
          name: this.formKey,
          data: msg
        })
      },
      cancelSubmitForm () {
        this.$store.commit('cancelSubmitForm', {name: this.formKey})
      },
      formError (msg) {
        if (msg.code == HttpCode.HTTP400) {
          for (let row of msg.errors) {
            let fields = this.$refs['updateForm'].fields.filter(it => {
              return it.prop == row.errName
            })
            if (fields.length > 0) {
              fields[0].validateMessage = row.errMsg
              fields[0].validateState = 'error'
            }
          }
        }
      }
    },
    props: {
      formKey: {
        type: String,
        default: 'currentForm'
      },
      roleList: {
        type: Array,
        default: []
      },
      menuList: {
        type: Array,
        default: []
      },
      data: {
        type: Object,
        default: {}
      }
    },
    computed: {
      submitFormEventListener () {
        return this.$store.getters.onEvent(`${this.formKey}-submitForm`)
      }
    },
    watch: {
      submitFormEventListener: function (a, b) {
        if (a == null || a == b) {
          return null
        }
        this.submitForm()
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }

  .avatar {
    width: 120px;
    height: 120px;
    display: block;
  }
</style>
